from database.MongoDb.Db import Db
from data_analyze import analyze
import pandas as pd
from main_progrom.const import FileConst


def get_data_list(file_name: str, sheet_name: str or int, header: int = 2):
    return pd.read_excel(file_name, sheet_name=sheet_name, header=header).loc[:]


def director(tmp_analyze: analyze.Analyze):
    tmp_analyze.deal_time()
    tmp_analyze.deal_data_list()
    exam.insert_many(tmp_analyze.data_format())


'''
    设置考试学期
'''


def set_semester():
    title = pd.read_excel(FileConst.file_name, sheet_name=0).iloc[0]
    data = {
        'semester': str(title.index[0]).split(' ')[1]
    }
    semester.insert_one(data)


'''
    设置上机考试
'''


def computer_test():
    # 从excel中读取数据
    computer_analyze = analyze.Analyze(get_data_list(FileConst.computer_test_file_name, 0), False)

    # print(data_list)
    computer_analyze.deal_time()
    computer_analyze.split_into_array(False)
    res_list: pd.DataFrame = computer_analyze.data_format()

    # 处理缺失数据
    res_list['type'] = '上机'

    # 将数据结果数据存入数据库
    exam.insert_many(res_list)


if __name__ == '__main__':
    exam: Db = Db("new_exam")
    semester: Db = Db("semester")

    week18_analyze = analyze.Analyze(get_data_list(FileConst.file_name, 1))
    week19_analyze = analyze.Analyze(get_data_list(FileConst.file_name, 2))
    director(week18_analyze)
    director(week19_analyze)
    set_semester()
    computer_test()
